package com.iptv.grape;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.iptv.grape.beans.ApkBean;
import com.iptv.grape.ui.adpt.AppMgrAdpt;
import com.iptv.grape.ui.dlg.DlgMgr;
import com.iptv.grape.util.ApkUtil;
import com.iptv.grape.view.ZoneGridView;

import java.util.List;

public class ApkMgrAct extends AppCompatActivity {
    private ZoneGridView mGridView;
    private TextView tvApkCount;
    private List<ApkBean> beans;
    private AppMgrAdpt adpt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_app_mgr);
        mGridView = findViewById(R.id.gvApkList);
        mGridView.setMySelector(R.drawable.img_back_fucs);
        mGridView.setMyScaleValues(1.15f, 1.1f);
//        mGridView.setMyVerticalSpacing(20);

        tvApkCount = findViewById(R.id.tvApkCount);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final ApkBean bean = (ApkBean) parent.getItemAtPosition(position);
                DlgMgr.apkTreatDlg(ApkMgrAct.this, bean.icon, new DlgMgr.Click() {
                    @Override
                    public void onListener(View view) {
                        switch (view.getId()) {
                            case R.id.btnOpenApk:
                                ApkUtil.openApk(ApkMgrAct.this, bean.packageName);
                                break;
                            case R.id.btnDelApk:
                                ApkUtil.uninstall(ApkMgrAct.this, bean.packageName);
                                udapteAdpt();
                                break;
                        }
                    }
                });
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        udapteAdpt();
    }

    public void udapteAdpt() {

        beans = ApkUtil.getAppInfosSort();
        if (beans == null || beans.size() < 1) {
            tvApkCount.setText("未检测到应用");
            return;
        }
        tvApkCount.setText("已安装" + beans.size() + "个");
        adpt = new AppMgrAdpt(ApkMgrAct.this, beans);
        mGridView.setAdapter(adpt);


    }
}
